const router = require('koa-router')()
const sendText = require('../robot')
const schedule = require('node-schedule')


// 普通消息
router.post("/mes", async (ctx) => {
  const { content } = ctx.request.body;
  ctx.body = {
    data: {
      code: 200,
      msg: "发送成功",
      content
    }
  };
  let text = `{\"text\":\"${content}\"}`
  sendText("text", text)
});

// 卡片消息
router.post("/card", async (ctx) => {
  const { title, text } = ctx.request.body;
  ctx.body = {
    data: {
      code: 200,
      msg: "发送成功",
      title,
      text
    }
  };
  let card = `{\"elements\": [{\"tag\": \"div\",\"text\": {\"content\": \"${text}\",\"tag\": \"plain_text\"}}],\"header\": {\"template\": \"blue\",\"title\": {\"content\": \"${title}\",\"tag\": \"plain_text\"}}}`
  sendText("interactive", card)
});

// 定时消息
router.post("/time", async (ctx) => {
  const { content, time } = ctx.request.body;
  ctx.body = {
    data: {
      code: 200,
      msg: "发送成功",
      content,
      time
    }
  };
  let text = `{\"text\":\"${content}\"}`
  schedule.scheduleJob(time, function () {
    sendText("text", text)
  })
});

module.exports = router

